<template>
  <el-dialog
    class="subject-status-add-record"
    :title="dialogTitle"
    width="600px"
    :show-close="true"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <div>
      <el-form :rules="rules" ref="formRef" size="mini" :model="formData" label-width="140px">
        <el-form-item prop="takeEffect" label="是否生效：">
          <el-radio-group v-model="formData.takeEffect">
            <el-radio :label="1">生效</el-radio>
            <el-radio :label="0">失效</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="method" label="比较方法：">
          <el-select v-model="formData.method" style="width: 80%">
            <el-option
              v-for="item in methodOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="moduleName" label="模块名称：">
          <el-select v-model="formData.moduleName" style="width: 80%" @change="handleModuleChange">
            <el-option
              v-for="item in moduleNameOptions"
              :key="item.code"
              :label="item.codeName"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="currentItem" label="当前项：">
          <el-select v-model="formData.currentItem" style="width: 80%">
            <el-option
              v-for="item in currentItemOptions"
              :key="item.code"
              :label="item.codeName"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="comparativeObjectArr" label="比较项：">
          <el-select v-model="formData.comparativeObjectArr" multiple style="width: 80%">
            <el-option
              v-for="item in comparativeObjectOptions"
              :key="item.code"
              :label="item.codeName"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="compareVariables" label="比较变量：">
          <el-select v-model="formData.compareVariables" style="width: 80%">
            <el-option
              v-for="item in compareVariablesOptions"
              :key="item.code"
              :label="item.codeName"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="lowerLimit" label="下限值：">
          <el-input v-model="formData.lowerLimit" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item prop="upperLimit" label="上限值：">
          <el-input v-model="formData.upperLimit" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item prop="way" label="质疑方式：">
          <el-select size="mini" v-model="formData.way" placeholder="请选择" style="width: 80%">
            <el-option
              v-for="item in questioningModeList"
              :key="item.value"
              :value="item.value"
              :label="item.text"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="dialogTitle === '复制'">
          <p style="color: red"><i class="el-icon-info"></i>复制前请先修改数据！</p>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
      <el-button :loading="loading" size="small" type="primary" @click="handleSave">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { cloneDeep } from 'lodash-es'
import { mapActions } from 'vuex'

export default {
  props: {
    editData: Object,
    dialogVisible: Boolean,
    dialogTitle: String,
    questioningModeList: Array,
    relationshipTableIdOptions: Array,
    searchParams: Object,
    filters: Object,
  },
  data() {
    return {
      rules: {},
      loading: false,
      defaultFormData: {
        takeEffect: '',
        method: '',
        moduleName: '',
        currentItem: '',
        comparativeObjectArr: [],
        comparativeObject: '',
        compareVariables: '',
        lowerLimit: '',
        upperLimit: '',
        way: '',
      },
      formData: {},
      methodOptions: [
        { label: '暂无', value: '0' },
        { label: '大于等于', value: '1' },
        { label: '大于', value: '2' },
        { label: '小于等于', value: '3' },
        { label: '小于', value: '4' },
        { label: '等于', value: '5' },
        { label: '不等于', value: '6' },
      ],
      moduleNameOptions: [],
      currentItemOptions: [],
      comparativeObjectOptions: [],
      compareVariablesOptions: [],
    }
  },

  watch: {
    dialogVisible(val) {
      if (val) {
        if (this.editData.comparativeObject) {
          this.editData.comparativeObjectArr = this.editData.comparativeObject.split(',')
        }
        this.formData = { ...this.editData }
        if (this.editData.moduleName) {
          Promise.all[(this.getModuleName(), this.getCurrentItem(this.editData.moduleName), this.getCompareVariables())]
        } else {
          Promise.all[(this.getModuleName(), this.getCurrentItem(), this.getCompareVariables())]
        }
      } else {
        this.formData = { ...this.defaultFormData }
      }
    },
  },

  methods: {
    ...mapActions('verificationRule', [
      'copySelectListEcCharactervalueTableCompareAPI',
      'editSelectListEcCharactervalueTableCompareAPI',
      'getModuleNameAPI',
      'getCurrentItemAPI',
      'getCompareVariablesAPI',
    ]),

    // 选择模块名称
    async handleModuleChange(val) {
      this.getCurrentItem(val)
    },

    // 获取模块名称
    async getModuleName() {
      const params = {
        ...this.searchParams,
        ...this.filters,
      }
      params.surface = this.editData.surface
      const res = await this.getModuleNameAPI({ params })
      this.moduleNameOptions = res.data
    },

    // 获取比较变量
    async getCurrentItem(codeName) {
      const params = {
        ...this.searchParams,
        ...this.filters,
      }
      params.surface = this.editData.surface
      params.codeName = codeName
      const res = await this.getCurrentItemAPI({ params })
      this.currentItemOptions = res.data
      this.comparativeObjectOptions = res.data
    },

    // 获取当前项和比较项
    async getCompareVariables() {
      const params = {
        ...this.searchParams,
        ...this.filters,
      }
      params.surface = this.editData.surface
      const res = await this.getCompareVariablesAPI({ params })
      this.compareVariablesOptions = res.data
    },

    // 关闭取消
    handleClose() {
      this.formData = { ...this.defaultFormData }
      this.formData.comparativeObjectArr.splice(0, this.formData.comparativeObjectArr.length)
      this.$emit('handleCancel')
    },

    // 保存提交
    handleSave() {
      this.loading = true
      this.$refs.formRef.validate(valid => {
        if (valid) {
          // 改变comparativeObject的数据形式
          const arr = cloneDeep(this.formData.comparativeObjectArr)
          this.formData.comparativeObject = arr.toString()
          if (this.dialogTitle === '编辑') {
            const data = {
              reqDTOList: [this.formData],
            }
            this.editSelectListEcCharactervalueTableCompareAPI({ data }).then(res => {
              const { code } = res
              if (code === 200) {
                this.$message({
                  message: '编辑成功',
                  type: 'success',
                })
                this.$refs.formRef.resetFields()
                this.formData.comparativeObjectArr.splice(0, this.formData.comparativeObjectArr.length)
                console.log(this.formData.comparativeObjectArr, 'comparativeObjectArr')
                this.$emit('handleCancel', 'refresh')
              }
              this.loading = false
            })
          } else {
            const data = {
              ...this.formData,
            }
            this.copySelectListEcCharactervalueTableCompareAPI({ data }).then(res => {
              const { code } = res
              if (code === 200) {
                this.$message({
                  message: '复制成功',
                  type: 'success',
                })
                this.$refs.formRef.resetFields()
                this.formData.comparativeObjectArr.splice(0, this.formData.comparativeObjectArr.length)
                this.$emit('handleCancel', 'refresh')
              }
              this.loading = false
            })
          }
        } else {
          this.loading = false
          return false
        }
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.span-dropdown-item {
  display: inline-block;
  width: 120%;
}
</style>
